package com.wiscamp.ninechapters.problems.adapter.resources.types;


import com.wiscamp.ninechapters.problems.application.dto.commands.types.CreateProblemTypeCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.types.UpdateProblemTypeCommand;
import com.wiscamp.ninechapters.problems.application.dto.vo.types.ProblemTypeVO;
import com.wiscamp.ninechapters.problems.application.dto.vo.types.QuestionTypeVO;
import com.wiscamp.ninechapters.problems.application.services.ProblemTypeAppService;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.application.command.DisableCommand;
import cube.ddd.application.command.EnableCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 问题类型服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/Problems")

@Tag(name = "题库-题型信息")
public class ProblemTypeController extends BaseController {

    private final ProblemTypeAppService problemTypeAppService;

    public ProblemTypeController(ProblemTypeAppService problemTypeAppService) {
        this.problemTypeAppService = problemTypeAppService;
    }

    /**
     * 通过题型编号获取题型
     *
     * @param request
     * @param response
     * @param typeId
     * @return
     */
    @GetMapping("ProblemTypes/{typeId}")
    @Operation(summary = "通过题型编号获取题型")
    @HandleResourceException
    public RestResponse<ProblemTypeVO> getProblemType(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                      @PathVariable("typeId") long typeId) {
        return ResourceUtils.getById(request, response, typeId, problemTypeAppService::getProblemType);
    }

    /**
     * 通过机构编号和题型代码获取题型
     *
     * @param typeCode
     * @return
     */
    @GetMapping("ProblemTypes/Code/{typeCode}")
    @Operation(summary = "通过机构编号和题型代码获取题型")
    @HandleResourceException
    public RestResponse<ProblemTypeVO> getProblemTypeByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                            @PathVariable("typeCode") String typeCode) {
        return ResourceUtils.getByText(request, response, typeCode, problemTypeAppService::getProblemTypeByCode);
    }


    /**
     * 通过机构编号和题型名称获取题型列表
     *
     * @param typeName
     * @return
     */
    @GetMapping("ProblemTypes/Name/{typeName}")
    @Operation(summary = "通过机构编号和题型名称获取题型列表")
    @HandleResourceException
    public RestResponse<Collection<ProblemTypeVO>> getProblemTypesByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                         @PathVariable("typeName") String typeName) {
        return ResourceUtils.getListByText(request, response, typeName, problemTypeAppService::getProblemTypesByName);
    }

    @GetMapping("ProblemTypes/Available")
    @Operation(summary = "通过机构编号和题型名称获取题型列表")
    @HandleResourceException
    public RestResponse<Collection<ProblemTypeVO>> getAvailableProblemTypes(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        return ResourceUtils.getList(request, response, problemTypeAppService::getAvailableProblemTypes);
    }

    /**
     * 通过题型编号删除题型
     *
     * @param command
     * @return
     */
    @PutMapping("ProblemTypes/Actions/Delete")
    @Operation(summary = "通过题型编号删除题型")
    @HandleResourceException
    public RestResponse<Boolean> deleteProblemType(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                   @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, problemTypeAppService::deleteProblemType);
    }

    @PutMapping("ProblemTypes/Actions/Enable")
    @Operation(summary = "通过题型编号启用题型")
    @HandleResourceException
    public RestResponse<Boolean> enableProblemType(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                   @RequestBody EnableCommand command) {
        return ResourceUtils.enable(request, response, command, problemTypeAppService::enableProblemType);
    }

    @PutMapping("ProblemTypes/Actions/Disable")
    @Operation(summary = "通过题型编号禁用题型")
    @HandleResourceException
    public RestResponse<Boolean> disableProblemType(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                    @RequestBody DisableCommand command) {
        return ResourceUtils.disable(request, response, command, problemTypeAppService::disableProblemType);
    }

    /**
     * 创建题型
     *
     * @param command
     * @return
     */
    @PostMapping("ProblemTypes")
    @Operation(summary = "创建题型")
    @HandleResourceException
    public RestResponse<ProblemTypeVO> createProblemType(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                         @RequestBody CreateProblemTypeCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, problemTypeAppService::createProblemType);
    }

    /**
     * 更新题型
     *
     * @param command
     * @return
     */
    @PutMapping("ProblemTypes")
    @Operation(summary = "更新题型")
    @HandleResourceException
    public RestResponse<ProblemTypeVO> updateProblemType(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                         @RequestBody UpdateProblemTypeCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, problemTypeAppService::updateProblemType);
    }

    @GetMapping("ProblemTypes/{typeId}/QuestionTypes")
    @Operation(summary = "通过题类型编号获取关联问题题型列表")
    @HandleResourceException
    public RestResponse<Collection<QuestionTypeVO>> getQuestionTypesByProblemType(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                                  @PathVariable("typeId") long typeId) {
        return ResourceUtils.getListById(request, response, typeId, problemTypeAppService::getQuestionTypesByProblemType);
    }
}
